package co.baiku.boot.security.handler;

import co.baiku.boot.common.message.Message;
import co.baiku.boot.common.tools.ServletTools;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 认证失败处理类
 */
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable {

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException e) {
        String body = Message.fail(HttpStatus.UNAUTHORIZED, e.getMessage()).json();
        ServletTools.printJson(response, body);
    }
}
